﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using LibraryONTrack.dto;

namespace LibraryONTrack.Persistence.DataMappers
{
	public class StudentManager : PersonMapper
	{
		private int _ordinal_teacher_id;

		private void InitializeMapper(IDataReader reader)
		{
			PopulateOrdinals(reader);
			_isInitialized = true;
		}

		public override void PopulateOrdinals(IDataReader reader)
		{
			base.PopulateOrdinals(reader);
			_ordinal_teacher_id = reader.GetOrdinal("teacher_id");
		}

		public override object GetData(System.Data.IDataReader reader)
		{
			// This is where we define the mapping between the object properties and the 
			// data columns. The convention that should be used is that the object property 
			// names are exactly the same as the column names. However if there is some 
			// compelling reason for the names to be different the mapping can be defined here.

			// We assume the reader has data and is already on the row that contains the data 
			//we need. We don't need to call read. As a general rule, assume that every field must 
			//be null  checked. If a field is null then the nullvalue for that  field has already 
			//been set by the DTO constructor, we don't have to change it. 
			if (!_isInitialized)
			{
				InitializeMapper(reader);
			}

			Student dto = (Student) base.GetData(reader);

			// Now we can load the data
			if (!reader.IsDBNull(_ordinal_teacher_id))
			{
				dto.TeacherId = reader.GetInt64(_ordinal_teacher_id);
			}

			return dto;
		}

		public override object GetInstance()
		{
			return new Student();
		}
	}
}
